package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewProdimCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCprodimMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xuxinshuai
 * @date 2018/01/19
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ViewCprodimCmdImpl extends CommandAdapter implements ViewProdimCmd {


    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewCprodimMapper viewCskuMapper = ApplicationContextHandle.getBean(ViewCprodimMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
//        log.debug("----------条码查询参数----------" + param);
        log.info("ViewCprodimCmdImpl--param-->{}",param);

        //{“code”:0,”message”:”success”,”data”:{objid:主表id,tablename:主表名},”ret”:{“objid”:12,tableitem1:[1,2,3]}}，


        if (null == param || !param.containsKey("vh")) {
            throw new NDSException("参数格式错误！");
        }

        JSONObject vhObj = param.getJSONObject("vh");
        if (null == vhObj || !vhObj.containsKey("ret")) {
            throw new NDSException("参数格式错误！");
        }
        JSONArray jsonArr = new JSONArray();

        JSONObject retObj = vhObj.getJSONObject("ret");
        if (null == retObj || !retObj.containsKey("PS_C_PRODIM_ITEM")) {
            vh.put("data", jsonArr);
            return vh;
        }
        JSONArray items = retObj.getJSONArray("PS_C_PRODIM_ITEM");
        if (null == items || items.size() <= 0) {
            vh.put("data", jsonArr);
            return vh;
        }

//        Object[] idArray = new Object[items.size()];
//        for (int i = 0; i < items.size(); i++) {
//            JSONObject item = items.getJSONObject(i);
//            Long id = item.getLongValue("ID");
//            idArray[i] = id;
//        }

        Long objid = retObj.getLongValue("objid");
        ArrayList objArray = new ArrayList();
        objArray.add(objid);
        HashMap hashMap = new HashMap();
        hashMap = viewCskuMapper.selectprodimsql(objArray.toArray());
        if (hashMap == null) {
            throw new NDSException("PS_C_PRODIM数据查询结果为空");
        }

//        log.debug("商品属性查询结果" + hashMap);
        String procolumn = TypeUtils.castToString(hashMap.get("AD_PROCOLUMN_NAME"));
        String prodimename = TypeUtils.castToString(hashMap.get("ENAME"));
        List<HashMap> mapList = new ArrayList<>();
        mapList = viewCskuMapper.selectprodimiteamsql(items.toArray());
        JSONArray jsonArray = listToJsonarray(mapList);
        JSONArray postArray = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject postJo = new JSONObject();

            postJo.put("AD_PROCOLUMN_NAME", procolumn);
            postJo.put("ENAME", prodimename);
            postJo.put("ITEM_ECODE", jsonObject.getString("ECODE"));
            postJo.put("ITEM_ENAME", jsonObject.getString("ENAME"));
            postJo.put("ITEM_ORDERNO", jsonObject.getString("ORDERNO"));
            postJo.put("ITEM_ISACTIVE", jsonObject.getString("ISACTIVE"));
            postJo.put("ITEM_MODIFIEDDATE", jsonObject.getString("MODIFIEDDATE"));
            postJo.put("ITEM_MODIFIERENAME", jsonObject.getString("MODIFIERENAME"));
            postArray.add(postJo);

        }
        vh.put("data", postArray);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("success"));
        return vh;
    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }

}
